<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/13
 * Time: 17:03
 */

namespace app\admin\controller;


use app\api\controller\Hwy;
use think\admin\Controller;
use think\App;

/**
 * 视频列表
 * Class VideoPlayback
 * @package app\admin\controller
 */
class VideoPlayback extends Controller
{
    /**
     * 设备表
     * @var string
     */
    protected $table = 'lg_equip';

    /**
     * 错误信息转化
     * @var array
     */
    protected $err_msg = [
        'Record list is empty.' => '记录列表为空'
    ];

    /**
     * 店铺id
     * @var mixed|string
     */
    protected $shop_id = '';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->shop_id = !empty(session('shop_info.shop_id')) ? session('shop_info.shop_id') : '';
    }

    /**
     * 视频列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function index()
    {
        $this->title = '视频列表';
        $data = $this->request->param();
        //数据权限
        $map = [];
        if (!empty($this->shop_id)) {
            $map[] = ['id', 'IN', $this->shop_id];
        }
        $shopList = $this->app->db->table('lg_distributor')->where($map)->field('id,name')->select();
        $this->assign(['shopList' => $shopList]);
        $query = $this->_query($this->table)->alias('e')
            ->join('lg_distributor d', 'e.shop_id=d.id', 'LEFT')
            ->field('e.*,d.name shop_name')
            ->order('id DESC');
        if (isset($data['shop_id']) && $data['shop_id'] !== '') {
            $query->where('e.shop_id', $data['shop_id']);
        }
        if (isset($data['name']) && !empty($data['name'])) {
            $query->whereLike('e.name', "%{$data['name']}%");
        }
        if (isset($data['equip_id']) && !empty($data['equip_id'])) {
            $query->whereLike('e.equip_id', "%{$data['equip_id']}%");
        }
        //数据权限
        if (!empty($this->shop_id)) {
            $query->whereIn('e.shop_id', $this->shop_id);
        }
        $query->page();
    }

    /**
     * 播放
     * @auth true
     * @login true
     */
    public function play()
    {
        $data = $this->request->param();
        if ($this->request->isPost()) {
            if (empty($data['time'])) {
                $this->error('请先选择时间');
            }
            $timeArr = explode(' - ', $data['time']);
            $startTime = str_replace(' ', '+', $timeArr[0]);
            $endTime = str_replace(' ', '+', $timeArr[1]);
            $res = (new Hwy($this->app))->getPlaybackUrl($data['device_id'], $data['channel_id'], $startTime, $endTime);
            if (isset($res['error_code'])) {
                $resMsg = $this->err_msg[$res['error_msg']] ?? $res['error_msg'];
                $this->error($resMsg);
            }
            $res['url'] = 'http://weihua.9999jt.com/#/index.html';
            $res['startTime'] = $startTime;
            $res['endTime'] = $endTime;
            $this->success('成功', $res);
        }
        $this->fetch('', [
            'device_id' => $data['device_id'],
            'channel_id' => $data['channel_id'],
        ]);
    }
}